package com.yhx.job;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.Set;

@Component
public class CleanExcel {
    @Value("${download.path}")
    private String path;
    @Value("${spring.redis.excel}")
    private String excel;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    //每天清理一次
    @Scheduled(cron = "0 * 23 * * * ")
    public void cleanexcel(){
        SetOperations<String, String> opsForSet = stringRedisTemplate.opsForSet();
        Set<String> keys = opsForSet.members(excel);
        System.out.println(keys);
        if(keys!=null || keys.size()>0){
            for (String key : keys) {
                File file = new File(path + key);
                boolean exists = file.exists();
                if (exists){
                    file.delete();
                }
            }
        }
        stringRedisTemplate.delete(excel);
        System.out.println("清除完毕");
    }
}
